package com.youlai.boot.factory.controller;

import com.youlai.boot.factory.service.WorkReportPackageService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.factory.model.form.WorkReportPackageForm;
import com.youlai.boot.factory.model.query.WorkReportPackageQuery;
import com.youlai.boot.factory.model.vo.WorkReportPackageVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 报工单-分包关联前端控制层
 *
 * @author youlaitech
 * @since 2025-10-13 13:37
 */
@Tag(name = "报工单-分包关联接口")
@RestController
@RequestMapping("/api/v1/work-report-package")
@RequiredArgsConstructor
public class WorkReportPackageController  {

    private final WorkReportPackageService workReportPackageService;

    @Operation(summary = "报工单-分包关联分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('factory:work-report-package:query')")
    public PageResult<WorkReportPackageVO> getWorkReportPackagePage(WorkReportPackageQuery queryParams ) {
        IPage<WorkReportPackageVO> result = workReportPackageService.getWorkReportPackagePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增报工单-分包关联")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('factory:work-report-package:add')")
    public Result<Void> saveWorkReportPackage(@RequestBody @Valid WorkReportPackageForm formData ) {
        boolean result = workReportPackageService.saveWorkReportPackage(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取报工单-分包关联表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('factory:work-report-package:edit')")
    public Result<WorkReportPackageForm> getWorkReportPackageForm(
        @Parameter(description = "报工单-分包关联ID") @PathVariable Long id
    ) {
        WorkReportPackageForm formData = workReportPackageService.getWorkReportPackageFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改报工单-分包关联")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('factory:work-report-package:edit')")
    public Result<Void> updateWorkReportPackage(
            @Parameter(description = "报工单-分包关联ID") @PathVariable Long id,
            @RequestBody @Validated WorkReportPackageForm formData
    ) {
        boolean result = workReportPackageService.updateWorkReportPackage(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除报工单-分包关联")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('factory:work-report-package:delete')")
    public Result<Void> deleteWorkReportPackages(
        @Parameter(description = "报工单-分包关联ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = workReportPackageService.deleteWorkReportPackages(ids);
        return Result.judge(result);
    }
}
